/*
 * Desktop.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.graphics.Dimension;
import com.cute4j.swing.graphics.Image;
import com.cute4j.swing.graphics.Rectangle;

/**
 * Desktop Class.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public final class Desktop {

// Private Static Fields. ------------------------------------------------------

  private static Dimension screenSize;
  private static int screenResolution;
  private static Rectangle desktopBounds;

// Static Initialization. ------------------------------------------------------

  static {
    Swing.initialize();
  }

// Private Constructor. --------------------------------------------------------

 /**
  * Class has no instances.
  */
  private Desktop() {
    // empty constructor body
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns the size of default screen.
   *
   * @return the size of default screen
   */
  public static Dimension getScreenSize() {
    synchronized (Swing.CLASS_LOCK) {
      if (screenSize == null) {
        Swing.disposedCheck();
        screenSize = _getScreenSize();
      }
      return screenSize;
    }
  }

  /**
   * Returns screen resolution.
   *
   * @return screen resolution
   */
  public static int getScreenResolution() {
    synchronized (Swing.CLASS_LOCK) {
      Swing.disposedCheck();
      if (screenResolution == 0) {
        screenResolution = _getScreenResolution();
      }
      return screenResolution;
    }
  }

  /**
   * Returns desktop bounds.
   *
   * @return desktop bounds
   */
  public static Rectangle getDesktopBounds() {
    synchronized (Swing.CLASS_LOCK) {
      if (desktopBounds == null) {
        Swing.disposedCheck();
        desktopBounds = _getDesktopBounds();
      }
      return desktopBounds;
    }
  }

  /**
   * Takes a screenshot of the specified component. If the component is
   * <tt>null</tt>, desktop screenshot is taken.
   *
   * @param component the component
   *
   * @return screen shot of the specified component
   */
  public static Image takeScreenShot(Component component) {
    return takeScreenShot(component, "PNG");
  }

  /**
   * Takes a screenshot of the specified component. If the component is
   * <tt>null</tt> desktop screenshot is taken.
   *
   * @param component the component
   * @param imageFormat the image format
   *
   * @return screen shot of the specified component
   */
  public static Image takeScreenShot(Component component,
                                     String imageFormat) {
    if ((component != null) && (component.isDisposed())) {
      throw new IllegalArgumentException("Component disposed");
    }
    if (imageFormat == null) {
      throw new IllegalArgumentException("Null image format");
    }
    Swing.disposedCheck();
    if (component == null) {
      return _takeScreenshot(0, imageFormat);
    }
    return _takeScreenshot(component._delegate, imageFormat);
  }

  // TODO: add other desktop methods like browse, launch etc...

// Package-Private Native Methods. ---------------------------------------------

  static native Dimension _getScreenSize();

  static native int _getScreenResolution();

  static native Rectangle _getDesktopBounds();

  static native SwingImage _takeScreenshot(long componentDelegate,
                                           String imageFormat);

}
